<?php
/**
 * @author Thibaut Cromphaut, Gaïtano Boeraeve, Mathias Helin, Dimitry Dierickx
 * @copyright  Copyright (c) 2011 Artevelde University College Ghent 
 */

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    /**
     * @todo Add comments, switch to English, clean up
     */
    protected function _initJQueryViewHelper()
    {
        $view = $this->_getView();
        $view->addHelperPath('ZendX/JQuery/View/Helper', 'ZendX_JQuery_View_Helper');
        $view->jQuery()->setVersion('1.7')->setUiVersion('1.8.16');
        $view->jQuery()->enable();
        $view->jQuery()->uiEnable();
    }
    
    protected function _initViewHelpers()
    {
        $this->bootstrap('db');
        $this->bootstrap('layout');
        $layout = $this->getResource('layout');
        $view = $layout->getView();
        $view->doctype('HTML5');
        $view->headMeta()->appendHttpEquiv('Content-Type', 'text/html;charset=utf-8');
        $view->headTitle('All-e')->setSeparator(' - ');
        $view->userline = $this->_getUserLine();
    }
    
    protected function _initBaseUrl()
    {
        $this->bootstrap("frontController");
        $front=$this->getResource("frontController");
        $request=new Zend_Controller_Request_Http();
        $front->setRequest($request);
    }

    protected function _initNavigationViewHelper()
    {
        $xml = APPLICATION_PATH . '/configs/navigation.xml';
        $pages = new Zend_Config_Xml($xml);
        $navigation = new Zend_Navigation($pages);
        $storage = Zend_Auth::getInstance()->getStorage()->read();
        $role = (isset($storage) && $storage->Role != null) ?  $storage->Role->Name : 'Guest';
        
        $view = $this->_getView();
        $view->navigation($navigation)
             ->setAcl( new Ahs_Acl() )
             ->setRole($role)
                ;            
    }
    
    protected function _getView($resource = 'layout')
    {
        $this->bootstrap($resource);
        $layout = $this->getResource($resource);
        $view = $layout->getView();

        return $view;
    }
    
    protected function _getUserLine()
    {
        // Base url
        $this->_initBaseUrl();
        $base_url           = Zend_Controller_Front::getInstance()->getBaseUrl();
        
        // Check for an active session
        if(Zend_Auth::getInstance()->hasIdentity())
        {
            // Get storage
            $storage            = Zend_Auth::getInstance()->getStorage()->read();
            // Get unread PMs
            $MessageMapper      = new Application_Model_MessageMapper();
            $number_unread_pm   = $MessageMapper->fetchUnreadMessagesCountForUser($storage->Id);
            

            return '<section class="user_name"><a href="'.$base_url.'/user/view/id/'.$storage->Id.'">'.$storage->Fullname.' ('.$storage->Role->Name.')</a></section>'
                 . '<section class="user_mess">'
                        . '<a href="'.$base_url.'/user/view/id/'.$storage->Id.'#messages" onclick="goToHashTab(\'messages\')">'
                            . 'You have '.$number_unread_pm.' unread messages'
                        . '</a></section>'
                 . '<section class="user_btns">'
                    . '<a href="'.$base_url.'/user/editprofile" class="user_btns-edit">Edit profile</a>'
                    . '<a href="'.$base_url.'/user/logout" class="user_btns-logout">Logout</a>'
                . '</section>';
        }
        else
        {
            return '<span class="notloggedin"><a href="'.$base_url.'/user/login">You are not logged in</a></span>';
        }
    }
}